﻿using System;
using JYAG.PassiveViewControlModel.Examples.UI.Views;

namespace JYAG.PassiveViewControlModel.Examples.UI.Presenters
{
    /// <summary>
    /// The Presenter class for a simple login form.
    /// </summary>
    public class SimpleLoginFormPresenter
    {
        private readonly ISimpleLoginFormView view;

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleLoginFormPresenter"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public SimpleLoginFormPresenter(ISimpleLoginFormView view)
        {
            this.view = view;
            InitializeView();
            HookUpEvents();
        }

        private void Login(string username, string password)
        {
            if (String.IsNullOrEmpty(username))
            {
                this.view.Result.Text = "Error: No username given.";
            } 
            else if (String.IsNullOrEmpty(password))
            {
                this.view.Result.Text = "Error: No password given.";
            } 
            else
            {
                //Power to the geeks!!
                var result = username.ToLower().Contains("geek");
                this.view.Result.Text = result ? "Welcome" : "No Rights";
            }
        }

        private void OnLoginClicked(object sender, EventArgs args)
        {
            var username = this.view.Username.Text;
            var password = this.view.Password.Text;

            Login(username, password);
        }
        

        private void HookUpEvents()
        {
            this.view.LogOn.Click += this.OnLoginClicked;
        }

        private void InitializeView()
        {
            this.view.Password.IsPassword = true;
        }
    }
}
